import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hari_curly/src/common/style.dart';
import 'package:path_provider/path_provider.dart';
import 'package:hari_curly/src/components/svg.dart';

import '../../../common/event.dart';
import '../../../states/device_info_state.dart';

class DeviceStatus extends StatefulWidget {
  const DeviceStatus({super.key});

  @override
  State<StatefulWidget> createState() => _DeviceStatusState();
}

class _DeviceStatusState extends State<DeviceStatus> {
  var winMap = {
    "low": Text('wind_type_desc'.tr(), style: TextStyle(fontSize: 16)),
    "middle": Text('wind_type_desc1'.tr(), style: TextStyle(fontSize: 16)),
    "high": Text('wind_type_desc2'.tr(), style: TextStyle(fontSize: 16)),
  };
  var tempMap = {
    "low": Text('temperature_type_desc'.tr(), style: TextStyle(fontSize: 16)),
    "middle": Text('temperature_type_desc1'.tr(), style: TextStyle(fontSize: 16)),
    "high": Text('temperature_type_desc2'.tr(), style: TextStyle(fontSize: 16)),
    "higher": Text('temperature_type_desc3'.tr(), style: TextStyle(fontSize: 16)),
  };
  late var _path="";


  @override
  void initState() {
    super.initState();
    eventBus.on<DeviceStatusRefresh>().listen((event) {
      if(mounted){
        setState(() {});
      }
    });
    eventBus.on<DeviceConnectStatusRefreshEvent>().listen((event) {
      if(mounted){
        setState(() {});
      }
    });
    _logPath();
  }
  _logPath() async{
    final Directory? downloadsDir = await getDownloadsDirectory();
    _path=downloadsDir!.path;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      constraints: BoxConstraints(minHeight: 100),
      margin: EdgeInsets.symmetric(vertical: 5, horizontal: 0),
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 0, horizontal: 4),
        child: Card(
          color: unitBgColor,
          child: Column(
            children: [
              Container(
                alignment: Alignment.topLeft,
                padding: EdgeInsets.all(8),
                margin: EdgeInsets.fromLTRB(0, 0, 0, 6),
                child: Text(
                  'status_title'.tr(),
                  style: TextStyle(fontSize: 16),
                ),
              ),
              Container(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(16, 0, 16, 16),
                  child: Row(
                    children: [
                      Expanded(
                        flex: 1,
                        // 宽度 50% 还有padding 不能超出屏幕
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          // 整体靠左
                          children: [
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              // 内部居中
                              children: [
                                SvgIcon.icon('fan'),
                                ?winMap[ context.read<DeviceInfoStatus>().state.wind],
                                Text(
                                  'wind_type'.tr(),
                                  style: TextStyle(
                                    fontSize: 12,
                                    color: Colors.white38,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          // 整体靠左
                          children: [
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              // 内部居中
                              children: [
                                SvgIcon.icon('cool_wind', size: 30),
                            ?tempMap[ context.read<DeviceInfoStatus>().state.temperature],
                                Text(
                                  'temperature_type'.tr(),
                                  style: TextStyle(
                                    fontSize: 12,
                                    color: Colors.white38,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
